<?php

namespace Wonfu\Core\Database;


use Monolog\Logger;
use Psr\Container\ContainerInterface;

class AtlasLogger
{
    /**
     * @var ContainerInterface
     */
    private $c;

    /**
     * @param ContainerInterface $c
     */
    public function setC(ContainerInterface $c): void
    {
        $this->c = $c;
    }

    public function __invoke(array $entry)
    {
        $sql = $entry['statement'];
        $sql = preg_replace(['/\s{1,}/', '/\n/'], [' ', ' '], $sql);
        $values = $entry['values'];
        $sql = preg_replace_callback('/:(__\d__)/', function ($matches) use ($values) {
            return $values[$matches[1]];
        }, $sql);
        $duration = sprintf('%.2f ms',$entry['duration']*1000);
        /** @var Logger $logger */
        $logger = $this->c->get(Logger::class);
        $logger->info('执行atlas', ['sql' => $sql,'duration'=>$duration]);
    }


}